<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Admission extends MY_Controller {
	private $new_catgory;
	//构造函数
	public function __construct()
	{
		parent::__construct();
		//获取new下的categorys
		$this->new_catgory = $this->home->get_category_by_id(44);
	}

	public function index(){
		$this->article();
	}

	//首页展示
	public function article()
	{
		$data = array();
		$data['cates'] = $this->new_catgory;
		$data['recentid'] = $this->uri->segment(3);
		//取得第一个元素
		if(is_numeric($data['recentid']))
		{
		    $tmp_cur_name = $this->db->get_where('category',array('cid'=>$data['recentid']))->row_array();
		    $data['cur_catname'] = $tmp_cur_name['catname'];
		}

		$data['catname'] = 'admission';
	

		$total = $this->home->get_count($data['recentid']);//总条数
		if($total == 1)
		{
			$data['ainfo'] = $this->home->get_ainfo_by_cid($data['recentid']);
			template('admission/ad_info',$data);
		}
		else
		{
	        //获取标签下 的文章列表   分页
			$cid = $this->uri->segment(4);
			$perpage = PER_PAGE;                           //每页显示条数
			
			$page = $this->input->get('page');
			$page = @intval($page);
			if($page<=1) $page = 1;
			$this->load->library('page_list',array('total'=>$total,'size'=>$perpage,'page'=>$page));
			$offset=$perpage*($page-1);
			$data['articles'] = $this->home->get_article_by_category($data['recentid'],$perpage,$offset);
	        $data['pagination'] = $this->page_list->display(site_url('admission/article/'.$data['recentid'].'?page=-page-'));

			template('admission/index',$data);
		}
	}

	public function info()
	{
		$data = array();
		$data['cates'] = $this->new_catgory;
		$data['recentid'] = $this->uri->segment(3);
		$data['catname'] = 'admission';
		$tmp_cur_name = $this->db->get_where('category',array('cid'=>$data['recentid']))->row_array();
		$data['cur_catname'] = $tmp_cur_name['catname'];

		$aid = $this->uri->segment(4);
		$data['ainfo'] = $this->home->get_ainfo_by_aid($aid);
		template('admission/article',$data);
	}


	//获取友情链接
	public function link()
	{
		//获取所有的友情链接
		$data = array();
		$data['cates'] = $this->new_catgory;
		$data['recentid'] = '60';
		$data['catname'] = 'admission';
		$data['cur_catname'] = 'link';
		$data['links'] = $this->home->get_all_links();
		template('admission/link',$data);	
	}

	public function courseregistration()
	{
		$data = array();
		$data['cates'] = $this->new_catgory;
		$data['recentid'] = '62';
		$data['catname'] = 'admission';
		$data['cur_catname'] = 'course registration';
		if(IS_POST)
		{
			$data = array();
			$re_name = $this->input->post('re_name');
			$re_number = $this->input->post('re_number');
			$code = $this->input->post('re_code');
			$cou_name = $this->input->post('re_cou_name');
			$instructor = $this->input->post('re_instructor');

			if(! empty($code[0]) && is_array($code))
			{
				foreach ($code as $k => $v) {
					if(! empty($v) && ! empty($cou_name[$k]) && ! empty($instructor[$k]))
					{
						$data[] = array(
							're_name'     	=>$re_name,
							're_number'   	=> $re_number,
							're_code'     	=> $v,
							're_cou_name' 	=>$cou_name[$k],
							're_instructor'	=>$instructor[$k],
							're_addtime'    =>time()
							);						
					}
					else
					{
						continue;
					}

				}
				if(! empty($data))
				{
					$flag = $this->db->insert_batch('regisiter', $data);
					if($flag)
					{
						$this->success('添加成功');
					}
					else
					{
						$this->error('添加失败，请重新填写');
					}
				}
				else
					$this->error('请至少填写一组内容');
			}
			else
			{
				$this->error('请至少填写一组内容');
			}
		}
		else
		{
			template('admission/register',$data);	
		}
	}

	public function evaluation()
	{
		session_start();
		$data = array();
		$data['cates'] = $this->new_catgory;
		$data['recentid'] = '63';
		$data['catname'] = 'admission';
		$data['cur_catname'] = 'evaluation';
		if(IS_POST)
		{
			$cou_name = $this->input->post('cou_name');
			$cou_ins_name = $this->input->post('cou_ins_name');
			$cou_number = $this->input->post('cou_number');
			$cou_stu_number = $this->input->post('cou_stu_number');
			if(! empty($cou_name) || ! empty($cou_ins_name) || ! empty($cou_number) || ! empty($cou_stu_number))
			{
				$data = $this->input->post();
				$flag = $this->db->insert('course_eva', $data);
				if($flag)
				{
					$this->success('添加成功');
				}
				else
				{
					$this->error('添加失败，请重新填写');
				}
			}
			else
			{
				$this->error('请内容填写完整');
			}
		}
		else
		{
			template('admission/evaluation',$data);	
		}
	}

}
